package cn.demographic.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.demographic.entity.Marriage;
import cn.demographic.entity.Employment;
import cn.demographic.entity.Landlord;
import cn.demographic.entity.Live;
import cn.demographic.entity.Livinginformation;
import cn.demographic.entity.Spouse;
import cn.demographic.entity.User;

import cn.demographic.service.PersonalInformationService;
import cn.demographic.service.ProvinceCityCountyService;
import cn.demographic.service.UpdateUserSpFaMaEmpLivingService;
import cn.demographic.service.ApplicanttableService;
import cn.demographic.service.DetailsService;

@RestController
@RequestMapping("/personalInformation")
public class PersonalInformationController {
	@Autowired
	private ApplicanttableService applicanttableService;
	@Autowired
	private PersonalInformationService personalInformationService;
	@Autowired
	private DetailsService detailsService;
	@Autowired
	private UpdateUserSpFaMaEmpLivingService updateUserSpFaMaEmpLivingService;
	@Autowired
	private ProvinceCityCountyService provinceCityCountyService;
	
	/**
	 * 配偶信息
	 * @param spouse
	 * @return
	 */
	@RequestMapping("/addSpouse")
	@ResponseBody
	public Map<String,Object> addSpouse(Spouse spouse,Live live,HttpSession session){
		session.setAttribute("spouse", spouse);
		session.setAttribute("liveSpouse", live);
		Map<String,Object> map = new HashMap<String, Object>();
		return map;
	}
	
	/**
	 * 添加计生信息表
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("/addMarriage")
	@ResponseBody
	public Map<String,Object> addMarriage(Marriage record,String measuress,
			String birthDates, HttpSession session) throws ParseException{
		//时间转换
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		if(measuress != null && measuress != "") {
			//措施时间
			record.setMeasures(dateFormat.parse(measuress));
		}
		if(birthDates != null && birthDates != "") {
			//出生日期
			record.setBirthDate(dateFormat.parse(birthDates));
		}
		session.setAttribute("record", record);
		Map<String,Object> map = new HashMap<String, Object>();
		return map;
	}
	 
	
	/**
	 * 添加个人信息+地址信息
	 * @param user
	 * @param live
	 * @throws ParseException 
	 * @returnUser user,
	 */
	@RequestMapping("/addPersonal")
	@ResponseBody
	public Map<String,Object> addPersonal(User user, Live live,String province2,
			String city2, String county2, String township2, String village2, 
			String gates2, String localtimes,HttpSession session
			) throws ParseException{
		if( localtimes != null && localtimes != "") {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			user.setLocaltime( dateFormat.parse(localtimes) );
		}
		Live live2 = new Live();
		live2.setProvince(province2);
		live2.setCity(city2);
		live2.setCounty(county2);
		live2.setTownship(township2);
		live2.setVillage(village2);
		live2.setGates(gates2);
		session.setAttribute("user", user);
		session.setAttribute("liveUser", live);
		session.setAttribute("liveUser2", live2);
		Map<String,Object> map = new HashMap<String, Object>();
		Live live3 = new Live();
		live3.setProvince( detailsService.findByCodeProvince(province2).getName() );
		live3.setCity( detailsService.findByCodeCity(city2).getName() );
		live3.setCounty( detailsService.findByCodeCounty(county2).getName() );
		live3.setTownship(township2);
		live3.setVillage(village2);
		live3.setGates(gates2);
		map.put("liveUser", live3);
		return map;
	}
	
	/**
	 * 添加居住信息
	 */
	@RequestMapping("/addlive")
	@ResponseBody
	public Map<String,Object> addliveinfo(Livinginformation livinginformation,
			Landlord landlord, Live live, HttpSession session) {
		personalInformationService.addliveinfo(livinginformation, landlord, live,session);
		Map<String,Object> map = new HashMap<String, Object>();
		return map;
	}
	
	
	/**
	 * 添加就业信息
	 * @param employment
	 * @param live
	 * @param session
	 * @return
	 */
	@RequestMapping("/Addemployment")
	@ResponseBody
	public Map<String,Object> Addemployment(Employment employment,Live live, HttpSession session) {
		session.setAttribute("employment", employment);
		session.setAttribute("liveEmployment", live);
		Map<String,Object> map = new HashMap<String, Object>();
		return map;
	}
	
	/**
	 * 添加同住家庭成员信息
	 * @return
	 */
	@RequestMapping(value = "/findFamily",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findFamily(Integer[] betweenId,String[] citizenship,
			String[] name,Integer[] gender,String[] birth,Integer[] certificate,
			Integer [] thisCity, HttpSession session){
		session.setAttribute("betweenId", betweenId);
		session.setAttribute("citizenship", citizenship);
		session.setAttribute("name", name);
		session.setAttribute("gender", gender);
		session.setAttribute("birth", birth);
		session.setAttribute("certificate", certificate);
		session.setAttribute("thisCity", thisCity);
		Map<String,Object> map = new HashMap<String, Object>();
		return map;
	}
	
	/**
	 * 修改同住家庭成员信息
	 * @return
	 */
	@RequestMapping("/updateFamily")
	@ResponseBody
	public Map<String,Object> updateFamily(String[] withId,String[] betweenId,
			String[] citizenship, String[] name,String[] gender,
			String[] birth,String[] certificate,
			String[] thisCity, HttpSession session){
		session.setAttribute("withId", withId);
		session.setAttribute("betweenId", betweenId);
		session.setAttribute("citizenship", citizenship);
		session.setAttribute("name", name);
		session.setAttribute("gender", gender);
		session.setAttribute("birth", birth);
		session.setAttribute("certificate", certificate);
		session.setAttribute("thisCity", thisCity);
		Map<String,Object> map = new HashMap<String, Object>();
		return map;
	}
	
	
	/**
	 * 修改，根据id查询数据，返回修改页面
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/finddatails")
	public Map<String,Object> finddatails(User user){
//		Map<String,Object> mapAddress = provinceCityCountyService.find();
		Map<String,Object> map = detailsService.DetailsFind(user);
//		map.put("cityList", mapAddress.get("cityList") );
//		map.put("provinceList", mapAddress.get("provinceList") );
//		map.put("countyList", mapAddress.get("countyList") );
		return map;
	}
	
	
	/**
	 * 修改个人信息,居住信息
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateUserSpFaMaEmpLiving")
	public Map<String , Object> updateUserSpFaMaEmpLiving(Livinginformation livinginformation,
			Landlord landlord,Integer landlordid, Live live, Integer[] liveid, HttpSession session){
		landlord.setId(landlordid);
		updateUserSpFaMaEmpLivingService.UpdateUserSpFaMaEmpLiving( livinginformation,
				 landlord,  live,  liveid,  session);
		Map<String , Object> map = new HashMap<String , Object>();
		return map;
	}
	
	
	
	
	
	
	
	/**
	 * 查询审核状态-审核中
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/findInTheReview")
	public Map<String , Object> findInTheReview(){
		Map<String , Object> map = new HashMap<String , Object>();
		List<User> applicanttableList = applicanttableService.findUserInTheReview();
		map.put("applicanttableList", applicanttableList);
		return map;
	}
	
	/**
	 * 查询审核状态-审核未通过
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/findAuditFailed")
	public Map<String , Object> findAuditFailed(){
		Map<String , Object> map = new HashMap<String , Object>();
		List<User> applicanttableList = applicanttableService.findUserAuditFailed();
		map.put("applicanttableList", applicanttableList);
		return map;
	}
	
	/**
	 * 查询审核状态-审核未通过
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/findApproved")
	public Map<String , Object> findApproved(){
		Map<String , Object> map = new HashMap<String , Object>();
		List<User> applicanttableList = applicanttableService.findUserapproved();
		map.put("applicanttableList", applicanttableList);
		return map;
	}
	
}

